package com.itheima.service.impl;

import com.itheima.dao.BlogDao;
import com.itheima.dao.BlogSortDao;
import com.itheima.dao.TagDao;
import com.itheima.entity.Result;
import com.itheima.service.IndexService;
import com.itheima.utils.DateUtils;
import com.sun.org.apache.bcel.internal.generic.NEW;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @Author: Felix(李鑫)
 * @Date: 2020/9/1 10:50
 * @Description:
 */

@Service
public class IndexServiceImpl implements IndexService {

    @Autowired
    private BlogDao blogDao;

    @Autowired
    private BlogSortDao blogSortDao;

    @Autowired
    private TagDao tagDao;

    /**
     * 获取一年内文章的点击数
     *
     * @return
     */
    @Override
    public Map getBlogContributeCount() {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Calendar c = Calendar.getInstance();
        //开始的时间
        Date endDate = c.getTime();
        String end = endDate.toLocaleString();

        //过去一年
        c.setTime(endDate);
        c.add(Calendar.YEAR, -1);
        Date beginDate = c.getTime();
        String begin = beginDate.toLocaleString();

        //返回给到前端的数据
        Map<String, List<Object>> map = new HashMap<String, List<Object>>();
        map.put("blogContributeCount",null);
        List blogContributeCount = new ArrayList<>();

        List contributeDate = new ArrayList<>();
        contributeDate.add(begin);
        contributeDate.add(end);
        map.put("contributeDate",contributeDate);

        //得到一年中的日期  [2019-09-01, 2019-09-02, 2019-09-03, 2019-09-04, 2019-09-05.......]
        List<String> dates = DateUtils.getDayBetweenDates(begin, end);

        //得到一年内文章的点击数[{DATE=2020-03-31, COUNT=1}, {DATE=2020-04-23, COUNT=1}, {DATE=2020-04-24, COUNT=1},.......]
        List<Map<String, Object>> blogContributeCountList = blogDao.getBlogContributeCount(format.format(beginDate), format.format(endDate));


        //将数据按照指定的格式存进map中
        for (String date : dates) {
            ArrayList<Object> list = new ArrayList<>();
            list.add(date);
            list.add(0);
            for (Map<String, Object> stringObjectMap : blogContributeCountList) {
                String date1 = (String) stringObjectMap.get("DATE");
                if (date1.equals(date)) {
                    list.set(1,stringObjectMap.get("COUNT"));
                }
            }
            blogContributeCount.add(list);
        }

        map.put("blogContributeCount",blogContributeCount);

        return map;
    }

    @Override
    public Result getBlogCountByBlogSort() {
        List<Map<String, Object>> blogCountByBlogSort = blogSortDao.getBlogCountByBlogSort();
        Result result =  new Result<>();
        result.setData(blogCountByBlogSort);
        return result;
    }

    @Override
    public Result getBlogCountByTag() {
        List<Map<String, Object>> blogCountByTag = tagDao.getBlogCountByTag();
        Result<Object> result = new Result<>();
        result.setData(blogCountByTag);
        return result;
    }
}
